/* Copyright (c) 2024 Huawei Technologies Co., Ltd.
openFuyao is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:
         http://license.coscl.org.cn/MulanPSL2
THIS SOFTWARE IS PROVIDED ON AN 'AS IS' BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details. */
import { containerRouterPrefix, ResponseCode } from '../../common/constants';
import { useParams, Link, useHistory, useLocation } from 'inula-router';
import { useEffect, useState, useCallback } from 'openinula';
import { Form, message, Select, Input, Popover, Space, Button } from 'antd';
import { updateColocationRule, deleteColocationRule } from '../../api/colocationApi';
import '../../styles/colocationRuleManagementPage.less';
import ExactForm from './ExactForm';
import LabelForm from './LabelForm';
import Dayjs from 'dayjs';
import { DownOutlined } from '@ant-design/icons';
import ColocationDeleteModal from '../../components/ColocationDeleteModal';
import { filterStatusName } from '../../utils/common';
import ToolTipComponent from '@/components/ToolTipComponent';
import { CloseOutlined } from '@ant-design/icons';

export default function DetailModel({ modelType }) {
  const param = useParams();
  const history = useHistory();
  const location = useLocation();
  const [colocationRuleExactEditForm] = Form.useForm();
  const [messageApi, contextHolder] = message.useMessage();
  const [lookIsEdit, setLookIsEdit] = useState(false);
  const [exactPopOpen, setExactPopOpen] = useState(false);
  const [deleteModal, setDeleteModal] = useState(false);

  // 数据部分
  const [exactDetailData, setExactDetailData] = useState([]); // 详情数据
  const [exactSelectType, setExactSelectType] = useState(location.state.detail[0].colocationType);
  const [isShow, setIsShow] = useState(true);
  const handleRulesExactShowChange = () => {
    setIsShow(false);
  };

  const handleExactPopOpenChange = (open) => {
    setExactPopOpen(open);
  };

  const handleDeleteExact = () => {
    setExactPopOpen(false);
    setDeleteModal(true);
  };

  // 获取colocationRuleList
  const getColocationRuleExactList = () => {
    const arr = location.state.detail[0].selectors;
    if (modelType === 'exact') {
      arr.map(item => {
        item.workLoadTypes = item.workLoadTypes.toString();
        return {
          ...item,
        };
      });
    } else {
      arr.map(item => {
        item.workLoadTypes = item.workLoadTypes.toString();
        item.key = item.selector.matchExpressions[0].key;
        item.value = item.selector.matchExpressions[0].values.toString();
        return {
          ...item,
        };
      });
    }
    setExactDetailData(arr);
    colocationRuleExactEditForm.setFieldValue('colocationRuleName', location.state.detail[0].ruleName);
    colocationRuleExactEditForm.setFieldValue('colocationRuleType', filterStatusName(exactSelectType));
  };

  // 改变selecte
  const handleChangeProperty = (e) => {
    setExactSelectType(e);
  };

  // 标签成功回调
  const handleLabelOk = async (data) => {
    const exactOptData = JSON.parse(JSON.stringify(data));
    const values = await colocationRuleExactEditForm.validateFields();
    let arr = [];
    if (values) {
      if (modelType === 'exact') {
        arr = exactOptData.map(item => {
          item.workLoadTypes = item.workLoadTypes.split(' ');
          item.name = item.name.trim().replace(/\s/g, '');
          return {
            ...item,
            selector: null,
          };
        });
      } else {
        arr = exactOptData.map(item => {
          item.workLoadTypes = item.workLoadTypes.split(' ');
          const tempKey = item.key.trim().replace(/\s/g, '');
          const tempValue = item.value.length > 1 ? item.value.trim().replace(/\s/g, '').split(',') : item.value.trim().replace(/\s/g, '');
          delete item.key;
          delete item.value;
          return {
            ...item,
            name: '',
            selector: { matchExpressions: [{ key: tempKey, operator: 'In', values: [...tempValue] }] },
          };
        });
      }
      const obj = {
        ruleName: colocationRuleExactEditForm.getFieldValue('colocationRuleName').trim().replace(/\s/g, ''),
        createTimestamp: Dayjs().format('YYYY-MM-DD HH:mm'),
        colocationType: exactSelectType,
        selectors: [...arr],
      };
      try {
        const res = await updateColocationRule(obj.ruleName, obj);
        if (res.status === ResponseCode.OK) {
          messageApi.success('修改成功');
          setTimeout(() => {
            history.go(-1);
          }, 2000);
        } else {
          messageApi.error('修改失败');
        }
      } catch (e) {
        messageApi.error(`接口异常${e.response.data.message}`);
      }
    }
  };

  // 标签失败回调
  const handleLabelCancel = () => {
    history.go(-1);
  };

  // 修改
  const handleExactToEdit = () => {
    setLookIsEdit(true);
  };

  // 删除确定
  const handleDeleteConfirmBack = async () => {
    // 此处接口
    try {
      const res = await deleteColocationRule(colocationRuleExactEditForm.getFieldValue('colocationRuleName'));
      if (res.status === ResponseCode.OK) {
        messageApi.success('删除成功');
        setTimeout(() => {
          history.go(-1);
        }, 1000);
      } else {
        messageApi.error('删除失败');
      }
    } catch (e) {
      messageApi.error('接口异常');
    }
    setDeleteModal(false);
  };

  // 删除取消
  const handleDeleteCancel = () => {
    setDeleteModal(false);
  };

  useEffect(() => {
    getColocationRuleExactList();
  }, [getColocationRuleExactList]);

  return <div className="child_content">
    {contextHolder}
    <div className='rules_detail_title'>
      <div style={{ marginRight: '64px' }}>
        <h3>{location.state.detail[0].ruleName}</h3>
      </div>
      <Popover placement='bottom'
        getPopupContainer={() => document.getElementsByClassName('rules_detail_title')[0]}
        className='rules_column_pop'
        content={
          <div className='col_button'>
            <Button type="link" onClick={handleExactToEdit} disabled={!param.onlyRead}>修改</Button>
            <Button type="link" onClick={handleDeleteExact}>删除</Button>
          </div>
        }
        open={exactPopOpen}
        onOpenChange={handleExactPopOpenChange}>
        <Button className='primary_btn'>操作 <DownOutlined className='small_margin_adjust' /></Button>
      </Popover>
    </div>
    <div className='rules_form_detail_content'>
      <div className={`${isShow ? '' : 'cant_show'}`}>
        <ToolTipComponent>
          <div className="daemonsetDetail" style={{ display: 'flex', justifyContent: 'space-between', alignItems: 'center' }}>
            <div className='promot-box'>设置规则后，下发满足匹配规则的工作负载会自动带有相关混部属性的标识。</div>
            <CloseOutlined onClick={handleRulesExactShowChange} />
          </div>
        </ToolTipComponent>
      </div>
      <div className={`${isShow ? 'normal_nocreate_rules_overflow_height' : 'rules_nocreate_overflow_height'}`}>
        <div className='rules_exact_detail_content_top'>
          <Form form={colocationRuleExactEditForm}
            initialValues={{
              colocationRuleType: exactSelectType,
            }}
            disabled={(param.onlyRead && !lookIsEdit) || (!param.onlyRead && lookIsEdit)}
          >
            <div className='rules_exact_detail_content_top_flex'>
              <Form.Item name="colocationRuleName" label="规则命名"
                rules={[
                  {
                    required: true,
                    message: `规则命名不能为空`,
                  },
                ]}
              >
                <Input placeholder='规则名称' className="rules_exact_detail_content_top_select" disabled />
              </Form.Item>
              <Form.Item name="colocationRuleType" label="混部属性">
                <Input placeholder='混部属性' className="rules_exact_detail_content_top_select" disabled />
              </Form.Item>
            </div>
          </Form>
        </div>

        {(param.onlyRead && !lookIsEdit) || (!param.onlyRead && lookIsEdit) ? <div>
          {modelType === 'exact' ? <ExactForm open={true} dataList={exactDetailData} callbackOk={handleLabelOk} callbackCancel={handleLabelCancel} isEdit={false} /> :
            <LabelForm open={true} dataList={exactDetailData} callbackOk={handleLabelOk} callbackCancel={handleLabelCancel} isEdit={false} />}
        </div> : <div>
          {modelType === 'exact' ? <ExactForm open={true} dataList={exactDetailData} callbackOk={handleLabelOk} callbackCancel={handleLabelCancel} isEdit={true} /> :
            <LabelForm open={true} dataList={exactDetailData} callbackOk={handleLabelOk} callbackCancel={handleLabelCancel} isEdit={true} />}
        </div>}
      </div>
    </div>
    <ColocationDeleteModal
      title="删除混部规则"
      open={deleteModal}
      cancelFn={handleDeleteCancel}
      content={[
        `确定将规则名称为 ${colocationRuleExactEditForm.getFieldValue('colocationRuleName')} 删除吗？`,
      ]}
      confirmFn={handleDeleteConfirmBack}
      leftText='删除'
    />
  </div>;
}